package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.bean.PageRes;
import com.example.demo.bean.sysdict.SysDictDataDO;
import com.example.demo.bean.sysdict.SysDictTypeDO;
import com.example.demo.exception.OtherException;
import com.example.demo.mapper.sysdict.SysDictDataMapper;
import com.example.demo.mapper.sysdict.SysDictTypeMapper;
import com.example.demo.service.ISysDictDataService;
import com.example.demo.dto.sysdict.SysDictDataPageReqVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictDataDO> implements ISysDictDataService {

    @Resource
    private SysDictDataMapper sysDictDataMapper;

    @Resource
    private SysDictTypeMapper sysDictTypeMapper;

    @Override
    public PageRes<SysDictDataDO> list(SysDictDataPageReqVO sysDictDataPageReqVO) {
        return sysDictDataMapper.selectPage(sysDictDataPageReqVO);
    }

    @Override
    public int addSysDictData(SysDictDataDO sysDictDataDO) {
        validDictTypeExist(sysDictDataDO.getDictType());
        validDictDataValueExist(sysDictDataDO.getDictType(), sysDictDataDO.getDictDataValue(), null);
        return sysDictDataMapper.insert(sysDictDataDO);
    }

    @Override
    public int deleteSysDictData(SysDictDataDO sysDictDataDO) {
        validDictDataExist(sysDictDataDO.getDictDataId());
        return sysDictDataMapper.deleteById(sysDictDataDO.getDictDataId());
    }

    @Override
    public int updateSysDictData(SysDictDataDO sysDictDataDO) {
        validDictDataExist(sysDictDataDO.getDictDataId());
        validDictDataValueExist(sysDictDataDO.getDictType(), sysDictDataDO.getDictDataValue(), sysDictDataDO.getDictDataId());
        return sysDictDataMapper.updateById(sysDictDataDO);
    }

    @Override
    public SysDictDataDO getSysDictData(SysDictDataDO sysDictDataDO) {
        validDictDataExist(sysDictDataDO.getDictDataId());
        QueryWrapper<SysDictDataDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_data_id", sysDictDataDO.getDictDataId());
        return sysDictDataMapper.selectOne(queryWrapper);
    }

    @Override
    public List<SysDictDataDO> listByType(String dictType) {
        QueryWrapper<SysDictDataDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_type",dictType);
        return sysDictDataMapper.selectList(queryWrapper);
    }

    private void validDictTypeExist(String dictType) {
        QueryWrapper<SysDictTypeDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_code", dictType);
        SysDictTypeDO exist = sysDictTypeMapper.selectOne(queryWrapper);
        if (exist == null) {
            throw new OtherException("字典编码不存在");
        }
    }

    private void validDictDataValueExist(String dictType, String dictDataValue, Long dictDataId) {
        QueryWrapper<SysDictDataDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_type", dictType);
        queryWrapper.eq("dict_data_value", dictDataValue);
        if (dictDataId != null) {
            queryWrapper.ne("dict_data_id", dictDataId);
        }
        SysDictDataDO sysDictDataDO = sysDictDataMapper.selectOne(queryWrapper);
        if (sysDictDataDO != null) {
            throw new OtherException("字典数据值已存在");
        }
    }

    private void validDictDataExist(Long dictDataId) {
        SysDictDataDO sysDictDataDO = sysDictDataMapper.selectById(dictDataId);
        if (sysDictDataDO == null) {
            throw new OtherException("字典数据不存在");
        }
    }
}
